package com.wmm.service;

import com.wmm.bean.PageBean;
import com.wmm.bean.Student;
import com.wmm.dao.StudentDao;
import com.wmm.factory.DaoFactory;

import java.util.List;

public class StudentService {

    private StudentDao studentDao;

    private ClassesService classesService = new ClassesService();

    public StudentService() {
        this.studentDao = DaoFactory.getStudentDao();
    }

    /**
     * 自定义成员方法实现根据参数指定的Student对象来调用DAO层实现学生增加功能
     * @param student
     * @return
     */
    public boolean addStudentServer(Student student){
        boolean flag = studentDao.addStudent(student);
        if (flag){
            boolean b = classesService.addNumberServer(student.getClassNo());
            return b;
        }else {
            return flag;
        }
    }

    /**
     * 自定义成员方法实现根据参数指定的Student对象来调用DAO层实现学生删除功能
     * @param studentno
     * @return
     */
    public boolean deleteStudentServer(String studentno,String classNo){
        boolean flag = studentDao.deleteStudent(studentno);
        if (flag){
            boolean b = classesService.reduceNumberServer(classNo);
            return b;
        }else {
            return flag;
        }
    }

    /**
     * 自定义成员方法实现根据参数指定的Student对象来调用DAO层实现学生更新功能
     * @param student
     * @return
     */
    public boolean updateStudentServer(Student student){ return studentDao.updateStudent(student);}

    /**
     * 自定义成员方法实现根据参数指定的Student对象来调用DAO层实现查询学生功能
     * @return
     */
    public List<Student> queryStudentServer(String id, String name){ return studentDao.queryStudent(id,name);}

    /**
     * 分页查询，发现全部的学生数据
     * @param _currentPage
     * @param _rows
     * @return
     */
    public PageBean<Student> findStudentByPage(String _currentPage, String _rows) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //1. 创建空的PageBean对象
        PageBean<Student> pb = new PageBean<Student>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3. 调用dao查询总记录数
        int totalCount = studentDao.findTotalCount();
        pb.setTotalCount(totalCount);
        //4. 调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<Student> list = studentDao.findByPage(start,rows);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows == 0) ? totalCount / rows : totalCount / rows + 1;
        pb.setTotalPage(totalPage);
        return pb;

    }


    /**
     * 分页查询，发现查询的学生数据
     * @param _currentPage
     * @param _rows
     * @return
     */
    public PageBean<Student> findQueryStudentByPage(String _currentPage, String _rows,String bystudentno, String byname) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //1. 创建空的PageBean对象
        PageBean<Student> pb = new PageBean<Student>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3. 调用dao查询总记录数
        int totalCount = studentDao.findqueryTotalCount(bystudentno, byname);
        pb.setTotalCount(totalCount);
        //4. 调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List<Student> list = studentDao.findqueryByPage(start,rows,bystudentno,byname);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows == 0) ? totalCount / rows : totalCount / rows + 1;
        pb.setTotalPage(totalPage);
        return pb;

    }


}
